package com.xctech.ipb.dpn.common.utils;

import java.util.UUID;

/**
 * 链路追踪上下文工具类
 */
public class TraceContext {
    
    private static final ThreadLocal<String> TRACE_ID_HOLDER = new ThreadLocal<>();
    
    /**
     * 生成新的链路ID
     */
    public static String generateTraceId() {
        return UUID.randomUUID().toString().replace("-", "");
    }
    
    /**
     * 获取当前线程的链路ID
     */
    public static String traceId() {
        String traceId = TRACE_ID_HOLDER.get();
        if (traceId == null) {
            traceId = generateTraceId();
            TRACE_ID_HOLDER.set(traceId);
        }
        return traceId;
    }
    
    /**
     * 设置链路ID
     */
    public static void setTraceId(String traceId) {
        TRACE_ID_HOLDER.set(traceId);
    }
    
    /**
     * 清除链路ID
     */
    public static void clear() {
        TRACE_ID_HOLDER.remove();
    }
} 